package com.epcs.controller.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.epcs.model.bo.EventcategoryBO;
import com.epcs.model.bo.FollowingBO;
import com.epcs.model.dao.EventCategoryDAO;
import com.epcs.model.dto.Account;
import com.epcs.model.dto.Eventcategory;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class EventCategoryAction extends ActionSupport implements SessionAware, ServletRequestAware {
	
	private String json;
	private String categoryID;
	private boolean following;	

	private SessionMap<String, Object> sessionMap;
	private HttpServletRequest request;


	public String getAllCategory(){
		// get user session
		Account account = (Account) sessionMap.get("USER_INFO");
		EventcategoryBO categoryBO = new EventcategoryBO();
		FollowingBO followBO = new FollowingBO();
		String result = "Success";
		
		// kiem tra xem co login chua, neu khac null la login roi
		if (account != null) {
			//get all category
			List<Eventcategory> systemCategoryList =  categoryBO.getAllEventCategory(false);
			// get followed category by user
			List<Integer> followedCategoryIDList = categoryBO.getAllFollowedCategoryID(account);
			
			if (systemCategoryList != null) {
				// lấy các category đang dc following tồn tại trong hệ thống (lấy hết ko theo user)
				List<Integer> systemCategoryIDList = new ArrayList<Integer>();
				for (int i = 0; i < systemCategoryList.size(); i++){
					systemCategoryIDList.add(systemCategoryList.get(i).getEventCategoryId());
				}
				// get number of following of this system
				// map chac chan khac null
				List<Object> objectList = followBO.getNumberOfFollowedCategoryByCategoryIDList(systemCategoryIDList);
				
				// sort lại systemCategoryList theo systemCategoryIDList dc return trong objectList
				List<Eventcategory> tempCategoryList =  new ArrayList<Eventcategory>();
				systemCategoryIDList = (List<Integer>) objectList.get(0);
				for (int i = 0; i < systemCategoryIDList.size(); i++){
					Integer tempID = systemCategoryIDList.get(i);
					boolean next = true;
					for (int j = 0; j < systemCategoryList.size() && next; j++){
						if (tempID.compareTo(systemCategoryList.get(j).getEventCategoryId()) == 0){
							tempCategoryList.add(systemCategoryList.get(j));
							next = false;
							systemCategoryList.remove(j);
						}
					}
				}
				systemCategoryList = tempCategoryList;
				
				request.setAttribute("SYSTEM_CATEGORY_LIST", systemCategoryList);
				request.setAttribute("FOLLOWED_CATEGORY_ID_LIST", followedCategoryIDList);
				request.setAttribute("NUMBER_OF_CATEGORY_FOLLOWING", objectList.get(1));
			} else {
				if (account.getRole().getRoleId().compareTo(1) == 0){
					result = "AddMoreCategory";
				} else {
					request.setAttribute("WARNING_MESSAGE", "Hệ thống chưa tồn tại bất kỳ thể loại nào! Xin bạn quay lại sau!");
				}
			}
			
		} else {
			result = "Unauthorized";
		}
		
		return result;
	}
	
	public String followingCategory() {
		FollowingBO followingBO = new FollowingBO();
		json = "Success";
		
		// get user session
		Account account = (Account) sessionMap.get("USER_INFO");
		
		// kiem tra xem co login chua, neu khac null la login roi
		if (account != null) {
			boolean result = true;
			// get category by id
			Eventcategory eventcategory = new EventCategoryDAO().getEventCategoryByID(Integer.parseInt(categoryID));
			
			if (following) {
				result = followingBO.followingEventOrCategory(eventcategory, account);				
			} else {
				result = followingBO.unfollowingEventOrCategory(eventcategory, account);
			}
			
			if (!result) {
				json = "Fail";
			}
			
		} else {
			// neu chua login thi login
			json = "Unauthorized";
		}

		return Action.SUCCESS;
	}
	
	public String getTopCategory(){
		// get user session
		Account account = (Account) sessionMap.get("USER_INFO");
		EventcategoryBO categoryBO = new EventcategoryBO();
		FollowingBO followBO = new FollowingBO();
		String result = "Success";
		
		// kiem tra xem co login chua, neu khac null la login roi
		if (account != null) {
			//get all category
			List<Eventcategory> systemCategoryList =  categoryBO.getAllEventCategory(false);
			// get followed category by user
			List<Integer> followedCategoryIDList = categoryBO.getAllFollowedCategoryID(account);
			
			//loại bỏ những category mình đã follow (dùng cho suggestion khi click new event)
			if (followedCategoryIDList != null){
				for (int i = 0; i < systemCategoryList.size(); i++){
					boolean next = true;
					for (int j = 0; j < followedCategoryIDList.size() && next; j++){
						if (systemCategoryList.get(i).getEventCategoryId().compareTo(followedCategoryIDList.get(j)) == 0){
							// cùng category id --> xóa đi
							systemCategoryList.remove(i);
							i = i - 1;
							next = false;
						}
					}
				}					
			}
						
			if (systemCategoryList != null) {
				// lấy các category đang dc following tồn tại trong hệ thống (lấy hết ko theo user)
				List<Integer> systemCategoryIDList = new ArrayList<Integer>();
				for (int i = 0; i < systemCategoryList.size(); i++){
					systemCategoryIDList.add(systemCategoryList.get(i).getEventCategoryId());
				}
				// get number of following of this system
				// map chac chan khac null
				List<Object> objectList = followBO.getNumberOfFollowedCategoryByCategoryIDList(systemCategoryIDList);
				
				// sort lại systemCategoryList theo systemCategoryIDList dc return trong objectList
				List<Eventcategory> tempCategoryList =  new ArrayList<Eventcategory>();
				systemCategoryIDList = (List<Integer>) objectList.get(0);
				for (int i = 0; i < systemCategoryIDList.size(); i++){
					Integer tempID = systemCategoryIDList.get(i);
					boolean next = true;
					for (int j = 0; j < systemCategoryList.size() && next; j++){
						if (tempID.compareTo(systemCategoryList.get(j).getEventCategoryId()) == 0){
							tempCategoryList.add(systemCategoryList.get(j));
							next = false;
							systemCategoryList.remove(j);
						}
					}
				}
				systemCategoryList = tempCategoryList;
				List<Eventcategory> eventcategories = new ArrayList<Eventcategory>();
				if (systemCategoryList != null) {
					if (systemCategoryList.size() < 6) {
						for (int i = 0; i < systemCategoryList.size(); i++) {
							eventcategories.add(systemCategoryList.get(i));
						}
					} else {
						for (int i = 0; i < 6; i++) {
							eventcategories.add(systemCategoryList.get(i));
						}
					}

				}
				request.setAttribute("SYSTEM_CATEGORY_LIST", eventcategories);
				request.setAttribute("FOLLOWED_CATEGORY_ID_LIST", followedCategoryIDList);
				request.setAttribute("NUMBER_OF_CATEGORY_FOLLOWING", objectList.get(1));
			} else {
				if (account.getRole().getRoleId().compareTo(1) == 0){
					result = "AddMoreCategory";
				} else {
					request.setAttribute("WARNING_MESSAGE", "Hệ thống chưa tồn tại bất kỳ thể loại nào! Xin bạn quay lại sau!");
				}
			}
			
		} else {
			result = "Unauthorized";
		}
		
		return result;
	}
//	private List<String> cblCategory;
//	public String followingCategory() {
//		FollowingBO followingBo = new FollowingBO();
//		Following follow = new Following();
//		FollowingDAO followingDAO = new FollowingDAO();
//		String result = "Success";
//		// get user session
//		Account account = (Account) sessionMap.get("USER_INFO");
//		// kiem tra xem co login chua, neu khac null la login roi
//		if (account != null) {
//			// get username
//			String username = account.getUsername();
//			//delete all following category by account
//			List<Following> Followinglist = new FollowingDAO().getAllCategoryFollowing(account);
//			if(Followinglist !=null){
//				for (int i = 0; i < Followinglist.size(); i++){
//					followingDAO.deleteFollowingCategorybyAccount(Followinglist.get(i));
//				}
//			}
//			//add following category
//			for (int i = 0; i < cblCategory.size(); i++){
//				Eventcategory cate = new EventCategoryDAO().getEventCategory(cblCategory.get(i));
//				follow.setAccount(account);
//				follow.setIdOfFollow(cate.getEventCategoryId());
//				follow.setTypeOfFollow("category");
//				followingBo.insertFollowingCategory(follow);
//			}
//		} else {
//			// neu chua login thi login
//			return "Unauthorized";
//		}
//		
//		return result;
//	}
//
//	public List<String> getCblCategory() {
//		return cblCategory;
//	}
//
//	public void setCblCategory(List<String> cblCategory) {
//		this.cblCategory = cblCategory;
//	}

	// ---------------- session --------------------//
	@Override
	public void setSession(Map<String, Object> sessionMap) {
		this.sessionMap = (SessionMap<String, Object>) sessionMap;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;

	}


	// ---------------- GETTER SETTER--------------//}
	
	public String getCategoryID() {
		return categoryID;
	}

	public void setCategoryID(String categoryID) {
		this.categoryID = categoryID;
	}

	public boolean getFollowing() {
		return following;
	}

	public void setFollowing(boolean following) {
		this.following = following;
	}

	public String getJson() {
		return json;
	}

	public void setJson(String json) {
		this.json = json;
	}

	
}